<?
session_start();

include('./../config/db_settings.php');
include('pms_inc.php');

$user = $_SESSION[$settings['session_prefix'].'user_id'];
//echo '$user:'.$user;
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
<head>
<title>Private Message Center</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../templates/<? echo $settings['template'] ?>/style.css" media="all" />
<script type="text/javascript">
function del_confirm()
{
var r=confirm("Are you sure you want to delete this message permanently?");
if (r==true)
  {
  document.forms['frm_1'].action="delete.php";
  return true;
  }
else
  {
  return false;
  }
}
</script>
</head>
<body>
<?     
    //Are they logged in or not?
    if(!$user)
        {
        echo "<br><p>You aren't logged in. Please log in first.</p><br>";
        }
        
    else
        {
        //We need to grab the msg_id variable from the URL.
        $msg_id = $_REQUEST['msg_id'];
        
        //Get all of the information about the message with and id number of the one sent through the URL
        $view_msg = mysql_query("SELECT a.*, b.user_name sender, c.user_name receiver FROM ".$db_settings['messages_table']." a 
        join ".$db_settings['userdata_table']." b on a.sender_id=b.user_id 
        join ".$db_settings['userdata_table']." c on a.receiver_id=c.user_id 
        WHERE a.id = '$msg_id'");
        $msg = mysql_fetch_array($view_msg);
        
        $receiver_id = $msg['receiver_id'];
        $sender = $msg['sender'];
        $receiver = $msg['receiver'];
        $sender_id = $msg['sender_id'];
        $subject = htmlspecialchars(stripslashes(base64_decode($msg['subject'])));
        $message = htmlspecialchars(stripslashes(base64_decode($msg['message'])));
        $pm_ts = $msg['pm_ts'];
        $received = $msg['received'];
        
        //If the person who is supposed to receive the message is the currently logged in user everything is good
        if($receiver_id == $user)
            {
            //The message was received, so lets update the message in the database so it wont show up as a new msg any more
            if($received=='0' || $received=='2'){
            	mysql_query("UPDATE ".$db_settings['messages_table']." SET received=received+1 WHERE id = '$msg_id'");
            }
            
            //Lets get the private message count, to display for the user
            $sql = mysql_query ("SELECT count(*) pm_count FROM ".$db_settings['messages_table']." WHERE receiver_id='$user'");
            $row = mysql_fetch_array ($sql);
            $pm_count = $row['pm_count'];
            
            //Calculate the percentage full
            $percent = $pm_count/'50';
            $percent = $percent * '100';
            
            //Now we will display the little navigation thing, the fullness of the inbox, then display message information stuff, like who its from, the subject, and the body
            ?>
            <br>
            <center>
            <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
            <b><p>Inbox: <? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
            </center>
            <br>
            
            <table width="80%">
              <tr>
                <td width="120px"><p>From:</p></td>
                <td width=""><p><a href = "<? echo "../index.php?mode=user&show_user=$sender_id"; ?>" target="htkou_showuser"><? echo $sender; ?></a>
                </p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Time:</p></td>
                <td width=""><p><? echo $pm_ts; ?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Subject:</p></td>
                <td width=""><p><? echo $subject; ?></p></td>
              </tr>
              
              <tr>    
                <td width="120px"><p>Message Body:</p></td>
                <td width=""><p><? echo $message; ?></p></td>
              </tr>
              <tr>    
                <td colspan="2">&nbsp;</td>
              </tr>
              
              <tr>    
                <td colspan="2"><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <form name="frm_1" id="frm_1" action="compose.php"	method="get">
								<input type="hidden" name="msg_id" value="<? echo $msg_id ?>">
								<input type="submit" value="Reply">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="submit" value="Delete" onclick="return del_confirm()">
                </form>
                </p></td>
              </tr>
              
              
              
            </table>
            </center>
            <?
            }
        //if the sender wants to review this msg, everything is still ok
        elseif($sender_id == $user)
            {
            //Now we will display the little navigation thing, the fullness of the inbox, then display message information stuff, like who its from, the subject, and the body
            ?>
            <br>
            <center>
            <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
            </center>
            <br>
            
            <table width="80%">
              <tr>
                <td width="120px"><p>From:</p></td>
                <td width=""><p><? echo $sender; ?></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>To:</p></td>
                <td width=""><p><a href = "<? echo "../index.php?mode=user&show_user=$receiver_id"; ?>" target="htkou_showuser"><? echo $receiver; ?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Time:</p></td>
                <td width=""><p><? echo $pm_ts; ?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Subject:</p></td>
                <td width=""><p><? echo $subject; ?></p></td>
              </tr>
              
              <tr>    
                <td width="120px"><p>Message Body:</p></td>
                <td width=""><p><? echo $message; ?></p></td>
              </tr>
              <tr>    
                <td colspan="2">&nbsp;</td>
              </tr>
              
            </table>
            </center>
            <?
            }
        //Everything is not good, someone tried to look at somone else's private message
        else
            {
            ?>
            <p>It appears you are trying to view someone else's private message. Please view your own private messages, or go away.</p>
            <?
            }
        }
    ?>
</body>
</html>